﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Licenta_Stomatologie.Forms;

namespace Licenta_Stomatologie.Panels
{
    public partial class PacientEditPanel : UserControl
    {
        int pacientId = 1;
        private String[] appType = new String[] { "Control", "Interventie medicala" };

        public PacientEditPanel()
        {
            InitializeComponent();
            getPacientData();
        }

        public PacientEditPanel(int id)
        {
            InitializeComponent();
            pacientId = id;
            getPacientData();
            getAppointments();
            getBills();
        }

        private void getAppointments()
        {
            ProjectDBEntities db = new ProjectDBEntities();
            DataTable dt = new DataTable("pacApps");

            DataColumn[] cols = new DataColumn[] { new DataColumn("id",typeof(int)), new DataColumn("Data",typeof(String)), new DataColumn("Ora Inceperii",typeof(String)), new DataColumn("Tip",typeof(String)) };

            dt.Columns.AddRange(cols);

            IEnumerable<Appointment> apps = db.Appointments.Where(a => a.pacientId == pacientId);

            if (apps.Count() == 0)
            {
                dgvProgramari.Visible = false;
            }
            else
            {
                dgvProgramari.Visible = true;

                foreach (Appointment app in apps)
                {
                    dt.Rows.Add(app.id, app.date.Value.ToShortDateString(), app.date.Value.ToShortTimeString(), appType[ (int) app.type]);
                }

                dgvProgramari.DataSource = dt;

                dgvProgramari.Columns["id"].Visible = false;
            }
        }

        private void getBills()
        {

        }

        private void getPacientData()
        {
            ProjectDBEntities db = new ProjectDBEntities();

            Pacient pac = db.Pacients.First(p => p.id == pacientId);

            DateTime dob = (DateTime)pac.dataNasterii;
            String niceDate = "( " + dob.ToShortDateString() + " )";

            lblPacientName.Text = pac.nume + " " + pac.prenume;
            lblPacientDOB.Text = niceDate;
            lblPacientAge.Text = DateTime.Now.Year - ((DateTime)pac.dataNasterii).Year + " ani";
            lblNumarDosar.Text = "Dosar numarul : " + pac.id;

            tboxCNP.Text = pac.cnp;
            tboxNume.Text = pac.nume;
            tboxPrenume.Text = pac.prenume;

            cboxSex.SelectedItem = pac.sex;
            dtpDOB.Value = (DateTime)pac.dataNasterii;
            cboxJudet.SelectedItem = pac.judet;

            tboxLocalitate.Text = pac.localitate;
            tboxAdresa.Text = pac.adresa;
            tboxTelFix.Text = pac.telefonFix;
            tboxTelMobil.Text = pac.telefonMobil;
            tboxEmail.Text = pac.eMail;    
        }

        private void editPacient()
        {
            if (!checkData())
            {
                MessageBox.Show(Helpers.ErrorMessages.ERR_EMPTY_FIELD);
                return;
            }

            ProjectDBEntities db = new ProjectDBEntities();

            Pacient pac = db.Pacients.First(p => p.id == pacientId);

            pac.cnp = tboxCNP.Text;
            pac.nume = tboxNume.Text;
            pac.prenume = tboxPrenume.Text;
            pac.sex = cboxSex.SelectedItem.ToString();
            pac.dataNasterii = dtpDOB.Value;
            pac.judet = cboxJudet.SelectedItem.ToString();
            pac.localitate = tboxLocalitate.Text;
            pac.adresa = tboxAdresa.Text;
            pac.telefonFix = tboxTelFix.Text;
            pac.telefonMobil = tboxTelMobil.Text;
            pac.eMail = tboxEmail.Text;

            db.SaveChanges();

            MessageBox.Show(Helpers.UserMessages.SUCCES_EDIT_PACIENT);
        }

        private bool checkData()
        {
            foreach (TextBox tb in this.Controls.OfType<TextBox>())
            {
                if (tb.Text.Length == 0)
                {
                    return false;
                }
            }
            foreach (ComboBox cb in this.Controls.OfType<ComboBox>())
            {
                if (((String)cb.SelectedItem).Length == 0)
                {
                    return false;
                }
            }
            return true;
        }

        private void btnSaveChanges_Click(object sender, EventArgs e)
        {
            editPacient();

            this.Controls.Clear();
            this.Controls.Add(new Panels.Dashboard());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(Helpers.UserMessages.PACIENT_EDIT_CANCEL, "Confirmation", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                this.Controls.Clear();
                this.Controls.Add(new Panels.Dashboard());
            }  
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("", "Confirmation", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                ProjectDBEntities db = new ProjectDBEntities();

                db.Pacients.Remove(db.Pacients.First(p => p.id == pacientId));

                db.SaveChanges();

                MessageBox.Show("Pacientul a fost sters cu succes");
            }
        }

        private void btnExportToPDF_Click(object sender, EventArgs e)
        {
            Helpers.PDFGenerator gen = new Helpers.PDFGenerator();

            if (gen.generatePacientFile(pacientId))
            {
                //MessageBox.Show("Succes");
            }
            else
            {
                //MessageBox.Show("Insucces");
            }
        }

        private void dgvProgramari_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int appId = (int) dgvProgramari.SelectedRows[0].Cells["id"].Value;

            this.Controls.Clear();
            this.Controls.Add( new Panels.AppointmentEditPanel(appId) );
        }

        private void cboxJudet_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form frmDiagnostic = new Diagnostic(pacientId);
            frmDiagnostic.ShowDialog();
        }
    }
}
